﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Laboration1Uppgift4.Model
{
    public class BallSimulation
    {
        /// <summary>
        /// Instans av ball
        /// </summary>
        public Model.Ball Ball { get; set; } 

        /// <summary>
        /// Standardkonstruktor
        /// </summary>
        public BallSimulation()
        {
            Ball = new Model.Ball(); 
        }  

        /// <summary>
        /// Kollision
        /// </summary>  
        internal void Update(double totalSeconds, int borderSize)
        {
            SetBallPosition(totalSeconds);

            CheckCollision(borderSize);
        }

        /// <summary>
        /// Flyttar bollen
        /// </summary>
        private void SetBallPosition(double totalSeconds)
        {
            Ball.BallPosition += Ball.BallSpeed * (float)totalSeconds;
        }

        /// <summary>
        /// Kontrollerar om det sker en kollision och ändrar då bollens position
        /// </summary>
        private void CheckCollision(int borderSize)
        {
            if (Ball.BallPosition.Y > 1 || Ball.BallPosition.Y < 0)
            {
  
                Ball.BallSpeed = new Vector2() { X = Ball.BallSpeed.X, Y = Ball.BallSpeed.Y * -1 };
            }

            if (Ball.BallPosition.X > 1 || Ball.BallPosition.X < 0)
            {
                Ball.BallSpeed = new Vector2() { X = Ball.BallSpeed.X * -1, Y = Ball.BallSpeed.Y };
            }
        }
    }
}
